<template>
  <div class='xtx-goods-page' v-if="goodsInfo">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodsInfo.categories[1].id}`">{{goodsInfo.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodsInfo.categories[0].id}`">{{goodsInfo.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem >{{goodsInfo.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
            <div class="goods-info">
        <!-- 商品左边 -->
        <div class="goods-info-left">
          <GoodsImage :images='goodsInfo.mainPictures'/>
          <GoodsSales />
        </div>
        <!-- 商品右边 -->
        <div class="goods-info-right">
          <GoodsName :goods='goodsInfo'/>
          <!-- 规格选择 -->
          <GoodsSku :goods='goodsInfo' skuId='' @change='getuskId'/>
          <!-- 数量+-组件 -->
          <XtxNumbox v-model="count" :min=1 :max='goodsInfo.inventory' label='数量' />
          <!-- 按钮组件 -->
          <XtxButton style="margin-top: 20px;" type='primary' @click="addCart">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId='$route.params.id'/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs :goods='goodsInfo'/>
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type='1' :goodsId='goodsInfo.id'/>
          <GoodsHot :type='2' :goodsId='goodsInfo.id'/>
        </div>
      </div>
    </div>
  </div>
  <div class="loading" v-else/>
</template>

<script>
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsImage from './components/goods-image.vue'
import { findGoods } from '@/api/product'
import { useRoute } from 'vue-router'
import { provide, ref, watch } from 'vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import XtxButton from '@/components/library/xtx-button.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import Message from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, XtxButton, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const route = useRoute()
    const goodsInfo = ref(null)
    watch(() => route.params.id, (id) => {
      if (route.path === `/product/${id}`) {
        goodsInfo.value = null
        findGoods(id).then(res => {
          goodsInfo.value = res.result
        })
      }
    }, { immediate: true })
    provide('goodsInfo', goodsInfo)
    const count = ref(1)
    // 子组件传过来的信息
    const getuskId = (val) => {
      if (val.skuId) {
        goodsInfo.value.inventory = val.inventory
        goodsInfo.value.oldPrice = val.oldPrice
        goodsInfo.value.price = val.price
        goodsInfo.value.MyuskId = val.skuId
        goodsInfo.value.attrsText = val.attrsText
      } else goodsInfo.value.MyuskId = ''
    }
    // 点击加入购物车按钮
    const store = useStore()
    const addCart = () => {
      if (!goodsInfo.value.MyuskId) return Message({ type: 'warn', text: '请选择完成规格' })
      const cartObj = {
        skuId: goodsInfo.value.MyuskId,
        name: goodsInfo.value.name,
        attrsText: goodsInfo.value.attrsText,
        picture: goodsInfo.value.mainPictures[0],
        price: goodsInfo.value.price,
        nowPrice: goodsInfo.value.price,
        selected: true,
        stock: goodsInfo.value.inventory || goodsInfo.value.stock,
        count: count.value,
        discount: 1,
        isCollect: false,
        isEffective: true,
        id: goodsInfo.value.id
      }
      store.dispatch('cart/addCart', cartObj).then(res => {
        Message({ type: 'success', text: '添加购物车成功' })
      })
    }
    return { goodsInfo, count, getuskId, addCart }
  }
}
</script>

<style scoped lang='less'>
.loading{
  height: 600px;
  background: #fff url('~@/assets/images/load.gif') no-repeat center;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  &-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  &-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
</style>
